package com.smart.manager.task;


import cn.hutool.core.collection.CollUtil;
import com.alicp.jetcache.Cache;
import com.smart.common.annotation.TaskBean;
import com.smart.common.annotation.TaskMethod;
import com.smart.common.cache.CacheService;
import com.smart.common.entities.domain.TaskResult;
import com.smart.manager.member.entities.enums.ChickenState;
import com.smart.manager.member.entities.models.MemberMaterialChicken;
import com.smart.manager.member.entities.models.query.QMemberMaterialChicken;
import com.smart.manager.member.entities.models.query.QMemberProfileMain;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模拟任务 有参和无参任务模拟
 *
 * @author Administrator
 * @date 2022/09/16
 */
@Log4j2
@Component
@TaskBean
@RequiredArgsConstructor
public class ChickenDelTask {

    private final CacheService cacheService;

    @TaskMethod(title = "鸡蛋回收")
    public synchronized TaskResult chickenDelTask() {
        LocalDateTime now = LocalDateTime.now();
        List<MemberMaterialChicken> chickens = new QMemberMaterialChicken().state.eq(ChickenState.L1).findList();
        List<MemberMaterialChicken> collect = chickens.stream().map(x -> x.setState(ChickenState.L4).setPickUpTime(now)).collect(Collectors.toList());
        DB.updateAll(collect);
        List<Long> list = new QMemberProfileMain().select(QMemberProfileMain.Alias.id).findSingleAttributeList();
        HashSet<Long> longs = new HashSet<>(list);
        cacheService.getLimitCache().removeAll(longs);
        return new TaskResult(true, "回收了"+ collect.size()+"鸡蛋");
    }


}
